/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.RequiresRole;
import com.hykj.xxltest.model.OrderheadModel;
import com.hykj.xxltest.service.OrderheadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author innel
 * @date 2018-07-12 14:08:40
 */
@Controller
@RequestMapping("/orderhead")
@Api(description = "")
public class OrderheadController {

    private static final Logger logger = Logger.getLogger(OrderheadController.class);

    @Autowired
    private OrderheadService orderheadService;

    /**
    * 列表
    */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取列表",
            notes = "备注：",
            response = OrderheadModel.class,
            httpMethod = "POST")
    public AppBack list(@ApiParam(value = "每页显示个数,不填默认10")
                        @RequestParam(required = false)
                                Integer pageSize,
                                                                @ApiParam(value = "userid")
                        @RequestParam(required = false)
                        Integer userid,
                                                        @ApiParam(value = "ordertype")
                        @RequestParam(required = false)
                        Integer ordertype,
                                                                            @ApiParam(value = "支付类型 ")
                        @RequestParam(required = false)
                        Integer paytype,
                                                                                                                    @ApiParam(value = "国id")
                        @RequestParam(required = false)
                        Integer countryid,
                                                        @ApiParam(value = "省id")
                        @RequestParam(required = false)
                        Integer provinceid,
                                                        @ApiParam(value = "cityid")
                        @RequestParam(required = false)
                        Integer cityid,
                                                        @ApiParam(value = "regionid")
                        @RequestParam(required = false)
                        Integer regionid,
                                                                                                                                                            @ApiParam(value = "订单状态")
                        @RequestParam(required = false)
                        Integer status,
                                                                                                                    @ApiParam(value = "配送员id")
                        @RequestParam(required = false)
                        Integer courierid,
                                                        @ApiParam(value = "优惠券id")
                        @RequestParam(required = false)
                        Integer discountid,
                                                                                            @ApiParam(value = "开始时间 范例 2017-11-06 16:02:11", hidden = true)
                        @RequestParam(required = false)
                                String startTime,
                        @ApiParam(value = "截止时间 格式 YYYYMMDD 也可以", hidden = true)
                        @RequestParam(required = false)
                                String endTime,
                        @ApiParam(value = "搜索关键字")
                        @RequestParam(required = false)
                                String searchKey,
                        @ApiParam(value = "是否是后台列表请求", hidden = true)
                        @RequestParam(required = false)
                                String backList,
                        @ApiParam(value = "是否简单显示", hidden = true)
                        @RequestParam(required = false)
                                String noD,
                        @ApiParam(value = "不填不分页")
                        @RequestParam(required = false)
                                Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", pageSize);
                                                        params.put("userid", userid);
                                                params.put("ordertype", ordertype);
                                                                    params.put("paytype", paytype);
                                                                                                            params.put("countryid", countryid);
                                                params.put("provinceid", provinceid);
                                                params.put("cityid", cityid);
                                                params.put("regionid", regionid);
                                                                                                                                                    params.put("status", status);
                                                                                                            params.put("courierid", courierid);
                                                params.put("discountid", discountid);
                                                                    params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("searchKey", searchKey);
        params.put("noD", noD);
        params.put("backList", backList);
        if (page == null) {
            return new AppBack(orderheadService.getDList(params));
        }
        PageInfo<OrderheadModel> p = orderheadService.getDList(params, page);
        return new AppBack(p);
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取",
            notes = "备注：",
            response = OrderheadModel.class,
            httpMethod = "POST")
    public AppBack info( 
            @ApiParam(value = "的ID", required = true)
            @RequestParam Integer orderheadId) {
            OrderheadModel orderhead = orderheadService.getByIdD(orderheadId);
        if (orderhead==null){
            return new AppBack(-1, "当前资源不存在");
        }
        return new AppBack(orderhead);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "保存",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack save(                                         
                         @ApiParam(value = "userid", required = true)
                         @RequestParam
                Integer userid,

                                
                         @ApiParam(value = "ordertype", required = true)
                         @RequestParam
                Integer ordertype,

                                
                         @ApiParam(value = "创建时间", required = true)
                         @RequestParam
                                 Date createtime,

                                
                         @ApiParam(value = "支付类型 ", required = true)
                         @RequestParam
                Integer paytype,

                                
                         @ApiParam(value = "订单号", required = true)
                         @RequestParam
                String orderno,

                                
                         @ApiParam(value = "总金额", required = true)
                         @RequestParam
                                 BigDecimal totalamount,

                                
                         @ApiParam(value = "实际支付", required = true)
                         @RequestParam
                BigDecimal realpay,

                                
                         @ApiParam(value = "国id", required = true)
                         @RequestParam
                Integer countryid,

                                
                         @ApiParam(value = "省id", required = true)
                         @RequestParam
                Integer provinceid,

                                
                         @ApiParam(value = "cityid", required = true)
                         @RequestParam
                Integer cityid,

                                
                         @ApiParam(value = "regionid", required = true)
                         @RequestParam
                Integer regionid,

                                
                         @ApiParam(value = "地址", required = true)
                         @RequestParam
                String address,

                                
                         @ApiParam(value = "收货人姓名", required = true)
                         @RequestParam
                String receivername,

                                
                         @ApiParam(value = "收货人电话", required = true)
                         @RequestParam
                String receiverphone,

                                
                         @ApiParam(value = "纬度", required = true)
                         @RequestParam
                BigDecimal lat,

                                
                         @ApiParam(value = "经度", required = true)
                         @RequestParam
                BigDecimal lon,

                                
                         @ApiParam(value = "订单状态", required = true)
                         @RequestParam
                Integer status,

                                
                         @ApiParam(value = "备注", required = true)
                         @RequestParam
                String memo,

                                
                         @ApiParam(value = "运费", required = true)
                         @RequestParam
                BigDecimal freight,

                                
                         @ApiParam(value = "优惠金额", required = true)
                         @RequestParam
                BigDecimal discount,

                                
                         @ApiParam(value = "配送员id", required = true)
                         @RequestParam
                Integer courierid,

                                
                         @ApiParam(value = "优惠券id", required = true)
                         @RequestParam
                Integer discountid,

                                
                         @ApiParam(value = "支付时间", required = true)
                         @RequestParam
                Date paytime,

                                
                         @ApiParam(value = "完成时间", required = true)
                         @RequestParam
                Date endtime,

                                             @ApiParam(value = "选填参数控制 默认不填", hidden = true)
                         @RequestParam(required = false)
                                 Integer extra) {
            OrderheadModel orderhead= new OrderheadModel();
                                        
                    orderhead.setUserid( userid);

                                
                    orderhead.setOrdertype( ordertype);

                                
                    orderhead.setCreatetime( createtime);

                                
                    orderhead.setPaytype( paytype);

                                
                    orderhead.setOrderno( orderno);

                                
                    orderhead.setTotalamount( totalamount);

                                
                    orderhead.setRealpay( realpay);

                                
                    orderhead.setCountryid( countryid);

                                
                    orderhead.setProvinceid( provinceid);

                                
                    orderhead.setCityid( cityid);

                                
                    orderhead.setRegionid( regionid);

                                
                    orderhead.setAddress( address);

                                
                    orderhead.setReceivername( receivername);

                                
                    orderhead.setReceiverphone( receiverphone);

                                
                    orderhead.setLat( lat);

                                
                    orderhead.setLon( lon);

                                
                    orderhead.setStatus( status);

                                
                    orderhead.setMemo( memo);

                                
                    orderhead.setFreight( freight);

                                
                    orderhead.setDiscount( discount);

                                
                    orderhead.setCourierid( courierid);

                                
                    orderhead.setDiscountid( discountid);

                                
                    orderhead.setPaytime( paytime);

                                
                    orderhead.setEndtime( endtime);

                    
            orderheadService.create(orderhead);
        return new AppBack();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "更新",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack update(                                                                  @ApiParam(value = "userid")
                          @RequestParam(required = false)
                Integer userid,
                                                          @ApiParam(value = "ordertype")
                          @RequestParam(required = false)
                Integer ordertype,
                                                          @ApiParam(value = "创建时间")
                          @RequestParam(required = false)
                Date createtime,
                                                          @ApiParam(value = "支付类型 ")
                          @RequestParam(required = false)
                Integer paytype,
                                                          @ApiParam(value = "订单号")
                          @RequestParam(required = false)
                String orderno,
                                                          @ApiParam(value = "总金额")
                          @RequestParam(required = false)
                BigDecimal totalamount,
                                                          @ApiParam(value = "实际支付")
                          @RequestParam(required = false)
                BigDecimal realpay,
                                                          @ApiParam(value = "国id")
                          @RequestParam(required = false)
                Integer countryid,
                                                          @ApiParam(value = "省id")
                          @RequestParam(required = false)
                Integer provinceid,
                                                          @ApiParam(value = "cityid")
                          @RequestParam(required = false)
                Integer cityid,
                                                          @ApiParam(value = "regionid")
                          @RequestParam(required = false)
                Integer regionid,
                                                          @ApiParam(value = "地址")
                          @RequestParam(required = false)
                String address,
                                                          @ApiParam(value = "收货人姓名")
                          @RequestParam(required = false)
                String receivername,
                                                          @ApiParam(value = "收货人电话")
                          @RequestParam(required = false)
                String receiverphone,
                                                          @ApiParam(value = "纬度")
                          @RequestParam(required = false)
                BigDecimal lat,
                                                          @ApiParam(value = "经度")
                          @RequestParam(required = false)
                BigDecimal lon,
                                                          @ApiParam(value = "订单状态")
                          @RequestParam(required = false)
                Integer status,
                                                          @ApiParam(value = "备注")
                          @RequestParam(required = false)
                String memo,
                                                          @ApiParam(value = "运费")
                          @RequestParam(required = false)
                BigDecimal freight,
                                                          @ApiParam(value = "优惠金额")
                          @RequestParam(required = false)
                BigDecimal discount,
                                                          @ApiParam(value = "配送员id")
                          @RequestParam(required = false)
                Integer courierid,
                                                          @ApiParam(value = "优惠券id")
                          @RequestParam(required = false)
                Integer discountid,
                                                          @ApiParam(value = "支付时间")
                          @RequestParam(required = false)
                Date paytime,
                                                          @ApiParam(value = "完成时间")
                          @RequestParam(required = false)
                Date endtime,
                                              @ApiParam(value = "的ID", required = true)
                          @RequestParam int orderheadId) {
            OrderheadModel orderhead= new OrderheadModel();
                                                            orderhead.setUserid( userid);
                                                    orderhead.setOrdertype( ordertype);
                                                    orderhead.setCreatetime( createtime);
                                                    orderhead.setPaytype( paytype);
                                                    orderhead.setOrderno( orderno);
                                                    orderhead.setTotalamount( totalamount);
                                                    orderhead.setRealpay( realpay);
                                                    orderhead.setCountryid( countryid);
                                                    orderhead.setProvinceid( provinceid);
                                                    orderhead.setCityid( cityid);
                                                    orderhead.setRegionid( regionid);
                                                    orderhead.setAddress( address);
                                                    orderhead.setReceivername( receivername);
                                                    orderhead.setReceiverphone( receiverphone);
                                                    orderhead.setLat( lat);
                                                    orderhead.setLon( lon);
                                                    orderhead.setStatus( status);
                                                    orderhead.setMemo( memo);
                                                    orderhead.setFreight( freight);
                                                    orderhead.setDiscount( discount);
                                                    orderhead.setCourierid( courierid);
                                                    orderhead.setDiscountid( discountid);
                                                    orderhead.setPaytime( paytime);
                                                    orderhead.setEndtime( endtime);
                                orderhead.setId(orderheadId);

            orderheadService.update(orderhead);
        return new AppBack();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack delete(                          @ApiParam(value = "的ID", required = true)
                          @RequestParam
                                  int  orderheadId) {

                        OrderheadModel newOrderhead= new OrderheadModel();
            newOrderhead.setStatus(-10);
            newOrderhead.setId(orderheadId);
                orderheadService.update(newOrderhead);
                return new AppBack();
    }

}
